using Microsoft.EntityFrameworkCore;
using OrderingSystem.Core.Entities;
using OrderingSystem.Core.Interfaces;
using OrderingSystem.Infrastructure.Data;

namespace OrderingSystem.Infrastructure.Services
{
    public class ReviewService : IReviewService
    {
        private readonly ApplicationDbContext _context;

        public ReviewService(ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task<IEnumerable<Review>> GetReviewsAsync()
        {
            // 使用EF Core查询而不是原始SQL查询
            // 直接返回查询结果，不尝试加载或设置不存在的属性
            return await _context.Reviews
                .Include(r => r.User)
                .OrderByDescending(r => r.addtime)
                .ToListAsync();
        }

        public async Task<IEnumerable<Review>> GetDishReviewsAsync(string dishName)
        {
            return await _context.Reviews
                .Include(r => r.User)
                .Where(r => r.caipinmingcheng == dishName)
                .OrderByDescending(r => r.addtime)
                .ToListAsync();
        }

        public async Task<Review> GetReviewAsync(long id)
        {
            // 直接返回查询结果，不尝试加载或设置不存在的属性
            return await _context.Reviews
                .Include(r => r.User)
                .FirstOrDefaultAsync(r => r.id == id);
        }

        public async Task<Review> CreateReviewAsync(Review review)
        {
            // 如果前端传入了菜品名称，我们可以尝试查找对应的菜品分类
            if (!string.IsNullOrEmpty(review.caipinmingcheng))
            {
                var dish = await _context.Dishes
                    .FirstOrDefaultAsync(d => d.caipinmingcheng == review.caipinmingcheng);
                
                if (dish != null)
                {
                    review.caipinfenlei = dish.caipinfenlei;
                }
            }

            review.addtime = DateTime.Now;
            _context.Reviews.Add(review);
            await _context.SaveChangesAsync();
            return review;
        }

        public async Task<Review> UpdateReviewAsync(Review review)
        {
            var existingReview = await _context.Reviews.FindAsync(review.id);
            if (existingReview == null)
            {
                return null;
            }

            existingReview.pingfen = review.pingfen;
            existingReview.pingjianeirong = review.pingjianeirong;
            await _context.SaveChangesAsync();
            return existingReview;
        }

        public async Task<bool> DeleteReviewAsync(long id)
        {
            var review = await _context.Reviews.FindAsync(id);
            if (review == null)
            {
                return false;
            }

            _context.Reviews.Remove(review);
            await _context.SaveChangesAsync();
            return true;
        }

        public async Task<Review> ReplyReviewAsync(long id, string reply)
        {
            var review = await _context.Reviews.FindAsync(id);
            if (review == null)
            {
                return null;
            }

            review.huifuneirong = reply;
            await _context.SaveChangesAsync();
            return review;
        }
    }
} 